#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 位串转8位字节串函数
void bitstring_to_bytes(char *bitstring, int blen, char *bytes) {
    int mlen = (blen + 7) / 8;
    for (int i = 0; i < mlen; i++) {
        bytes[i] = 0;
        for (int j = 0; j < 8; j++) {
            int index = blen - 8 * (mlen - 1 - i) - j - 1;
            if (index >= 0 && bitstring[index] == '1') {
                bytes[i] |= 1 << j;
            }
        }
    }
}

int main() {
    char bitstring[1000];
    char bytes[1000];
    int blen;

    printf("请输入位串长度：");
    scanf("%d", &blen);

    printf("请输入位串：");
    scanf("%s", bitstring);

    bitstring_to_bytes(bitstring, blen, bytes);

    int mlen = (blen + 7) / 8;
    printf("转换后的字节串：");
    for (int i = 0; i < mlen; i++) {
        printf("%02X ", (unsigned char)bytes[i]);
    }
    printf("\n");

    return 0;
}
